package pl.koziolekweb.szkola.GKMVCLista1;

// GK-MVCLista1/ModifListExample.java
 
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;

public class ModifListExample extends JFrame {
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public static void main(String[] args) {
        new ModifListExample();
    }

    ModifListExample() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setTitle("Przyk\u0142ad listy modyfikowalnej");
        setContentPane(new MainPanel());
        pack();
        setVisible(true);
    }
}

class MainPanel extends    JPanel
                implements ListSelectionListener {

    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	JTextArea infoArea;
    LModel    model;
    JList     lista;
    int       licznik;

    MainPanel() {
        model = new LModel();
        lista = new JList(model);
        lista.setSelectionMode(
                ListSelectionModel.SINGLE_SELECTION);
        lista.setSelectedIndex(0);
        lista.addListSelectionListener(this);
        lista.setFont(new Font("Serif",Font.BOLD,16));

        JScrollPane scrollOsob =
            new JScrollPane(lista,
                JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

        infoArea = new JTextArea(10,14);
        infoArea.setFont(new Font("Serif",Font.BOLD,16));
        infoArea.setBackground(Color.blue);
        infoArea.setForeground(Color.white);
        JScrollPane scrollInfo =
            new JScrollPane(infoArea,
                JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

        JSplitPane split =
            new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
                           scrollInfo,scrollOsob);
        split.setOneTouchExpandable(true);
        split.setDividerLocation(0.40);

        int   width = 200, height = 100;
        float xjust = Component.CENTER_ALIGNMENT;

        JButton bUsun = new JButton(
            new AbstractAction("Usu\u0144") {
                /**
				 * 
				 */
				private static final long serialVersionUID = 1L;

				public void actionPerformed(ActionEvent e) {
                    if (lista.isSelectionEmpty()) return;
                    int i = lista.getSelectedIndex();
                    model.remove(i);
                    if (!model.isEmpty()) select(
                            Math.min(i,model.getSize()-1));
                }
            }
        );
        configButton(bUsun,width,height,xjust);

        JButton bDodajZa = new JButton(
            new AbstractAction("Dodaj za") {
                /**
				 * 
				 */
				private static final long serialVersionUID = 1L;

				public void actionPerformed(ActionEvent e) {
                    if (lista.isSelectionEmpty()) return;
                    int i = lista.getSelectedIndex();
                    model.add(i+1,new Osoba("Nowa " +
                                    ++licznik,3*licznik));
                    select(i);
                }
            }
        );
        configButton(bDodajZa,width,height,xjust);

        JButton bDodajPrzed =
            new JButton(new AbstractAction("Dodaj przed") {
                /**
				 * 
				 */
				private static final long serialVersionUID = 1L;

				public void actionPerformed(ActionEvent e) {
                    if (lista.isSelectionEmpty()) return;
                    int i = lista.getSelectedIndex();
                    model.add(i,new Osoba("Nowa " +
                                    ++licznik,3*licznik));
                    select(i);
                }
            }
        );
        configButton(bDodajPrzed,width,height,xjust);

        JButton bDodajKon = new JButton(
            new AbstractAction("Dodaj na koniec") {
                /**
				 * 
				 */
				private static final long serialVersionUID = 1L;

				public void actionPerformed(ActionEvent e) {
                    model.add(new Osoba("Nowa " +
                                    ++licznik,3*licznik));
                    select(model.getSize()-1);
                }
            }
        );
        configButton(bDodajKon,width,height,xjust);

        JButton bWstaw = new JButton(
            new AbstractAction("Wstaw inn\u0105") {
                /**
				 * 
				 */
				private static final long serialVersionUID = 1L;

				public void actionPerformed(ActionEvent e) {
                    if (lista.isSelectionEmpty()) return;
                    int i = lista.getSelectedIndex();
                    model.set(i,new Osoba("Nowa " +
                                    ++licznik,3*licznik));
                    select(i);
                }
            }
        );
        configButton(bWstaw,width,height,xjust);

        JPanel box = new JPanel();
        Dimension rig = new Dimension(1,5);
        box.setLayout(new BoxLayout(box,BoxLayout.Y_AXIS));
        box.add(Box.createVerticalGlue());
        box.add(bUsun);
        box.add(Box.createRigidArea(rig));
        box.add(bDodajZa);
        box.add(Box.createRigidArea(rig));
        box.add(bDodajPrzed);
        box.add(Box.createRigidArea(rig));
        box.add(bDodajKon);
        box.add(Box.createRigidArea(rig));
        box.add(bWstaw);
        box.add(Box.createVerticalGlue());

        setLayout(new BorderLayout());
        add(box,  BorderLayout.EAST);
        add(split,BorderLayout.CENTER);

        select(0);
    }

    void configButton(JButton b, int w, int h, float just) {
        b.setMaximumSize(new Dimension(w,h));
        b.setAlignmentX(just);
    }

    void select(int i) {
        lista.ensureIndexIsVisible(i);
        lista.setSelectedIndex(i);
        infoArea.setText(
                ((Osoba)model.getElementAt(i)).getDane());
        infoArea.setCaretPosition(0);
    }

    public void valueChanged(ListSelectionEvent e) {
        if (e.getValueIsAdjusting() ||
            lista.isSelectionEmpty()) return;
        select(lista.getSelectedIndex());
    }
}

class LModel extends AbstractListModel {

    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	ArrayList<Osoba> dane  = new ArrayList<Osoba>();
    int size;

    LModel() {
        dane.add(new Osoba("Kowalski",27));
        dane.add(new Osoba("Malinowski",21));
        dane.add(new Osoba("Nowak",18));
        dane.add(new Osoba("Tarski",61));
        dane.add(new Osoba("Zaorski",43));
        dane.add(new Osoba("Maj",32));
        size = dane.size();
    }

    public void add(Osoba o) {
        dane.add(o);
        fireIntervalAdded(this,size-1,size-1);
    }

    public void add(int i, Osoba o) {
        if (size > 0)
            dane.add(i,o);
        else
            dane.add(o);
        fireIntervalAdded(this,i,i);
    }

    public void remove(int i) {
        dane.remove(i);
        fireIntervalRemoved(this,i,i);
    }

    public void set(int i, Osoba o) {
        dane.set(i,o);
        fireContentsChanged(this,i,i);
    }

    public Object getElementAt(int i) {
        return dane.get(i);
    }

    public int getSize() {
        return dane.size();
    }

    public boolean isEmpty() {
        return dane.isEmpty();
    }
}

class Osoba {

    private String nazwisko;
    private int    wiek;
    private String dane;

    Osoba(String _nazwisko, int _wiek) {
        nazwisko = _nazwisko;
        wiek     = _wiek;
        dane     = nazwisko + " " + wiek + "\nTo jest\nop" +
                  "is\nosoby\no nazwisku\n" + nazwisko +
                  "\nAdres: adres...\nTelefon: telefon..." +
                  "\ni\ntak\ndalej...";
    }

    public String toString() {
        return nazwisko;
    }

    String getDane() {
        return dane;
    }
}
